# import sys
#
# # 这里我们提供必要的引用。基本控件位于pyqt5.qtwidgets模块中。
# from PyQt5.QtWidgets import QApplication, QWidget
#
# if __name__ == '__main__':
#     # 每一pyqt5应用程序必须创建一个应用程序对象。sys.argv参数是一个列表，从命令行输入参数。
#     app = QApplication(sys.argv)
#     # QWidget部件是pyqt5所有用户界面对象的基类。他为QWidget提供默认构造函数。默认构造函数没有父类。
#     w = QWidget()
#     # resize()方法调整窗口的大小。这离是250px宽150px高
#     w.resize(250, 150)
#     # move()方法移动窗口在屏幕上的位置到x = 300，y = 300坐标。
#     w.move(300, 300)
#     # 设置窗口的标题
#     w.setWindowTitle('Simple')
#     # 显示在屏幕上
#     w.show()
#
#     # 系统exit()方法确保应用程序干净的退出
#     # 的exec_()方法有下划线。因为执行是一个Python关键词。因此，exec_()代替
#     sys.exit(app.exec_())
#
#

import sys

from PyQt5.QtCore import QRect, pyqtSignal
from PyQt5.QtWidgets import (QWidget, QToolTip,
                             QPushButton, QApplication, QLayout, QHBoxLayout, QLabel, QLineEdit, QVBoxLayout,
                             QMainWindow, QStatusBar, QDialog, QMessageBox)
from PyQt5.QtGui import QFont

from gglib.client import GGChatClient, GGChatDelegate


class GGChatWindow(QWidget, GGChatDelegate):
    def on_connected(self, client):
        pass

    def on_disconnected(self, client):
        pass

    def on_logged(self, client, **kwargs):
        pass

    def on_messaged(self, client, **kwargs):
        pass

    def on_logout(self, client):
        pass

    def on_exception(self, exception):
        pass

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


class GGLoginWindow(QWidget, GGChatDelegate):
    sigStatus = pyqtSignal(str)
    sigEnableWindow = pyqtSignal()
    sigLogged = pyqtSignal(object)

    def on_connected(self, client):
        self.sigStatus.emit("连接到服务器")

    def on_disconnected(self, client):
        self.sigStatus.emit("和服务器断开")

    def on_logged(self, client, **kwargs):
        self.sigEnableWindow.emit()
        self.sigLogged.emit(self.client)
        self.close()

    def on_messaged(self, client, **kwargs):
        pass

    def on_logout(self, client):
        pass

    def on_exception(self, exception):
        self.sigEnableWindow.emit()
        self.statusBar.showMessage(str(exception))

    def __init__(self, client=None):
        super().__init__()
        self.initUI()
        self.client = client
        if self.client:
            self.client.delegate = self
        self.sigStatus.connect(self.showNetworkStatus)

    def initUI(self):
        # 这种静态的方法设置一个用于显示工具提示的字体。我们使用10px滑体字体。

        # QToolTip.setFont(QFont('SansSerif', 10))
        self.setFixedSize(300, 200)
        user_layout = QHBoxLayout()
        lblUserName = QLabel("用户名")
        self.txtUserName = QLineEdit()
        user_layout.addWidget(lblUserName)
        user_layout.addWidget(self.txtUserName)
        pass_layout = QHBoxLayout()
        lblPassword = QLabel("密码")
        self.txtPassword = QLineEdit()
        pass_layout.addWidget(lblPassword)
        pass_layout.addWidget(self.txtPassword)

        vLayout = QVBoxLayout()
        vLayout.addLayout(user_layout)
        vLayout.addLayout(pass_layout)

        self.statusBar = QStatusBar(self)
        self.statusBar.setGeometry(0, self.height() - 30, self.width(), 30)
        btnLayout = QHBoxLayout()
        self.btnCancel = QPushButton("取消")
        self.btnLogin = QPushButton("登录")
        btnLayout.addWidget(self.btnCancel)
        btnLayout.addWidget(self.btnLogin)
        vLayout.addLayout(btnLayout)

        self.btnCancel.clicked.connect(self.close)
        self.btnLogin.clicked.connect(self.login)

        self.setLayout(vLayout)
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('登录')
        self.show()

    def showNetworkStatus(self, status_str):
        self.statusBar.showMessage(status_str)

    def login(self):
        username = self.txtUserName.text().strip()
        password = self.txtPassword.text().strip()
        if len(username) == 0 or len(password) == 0:
            QMessageBox.warning(self, "错误", "用户名或密码不能为空", QMessageBox.Yes)
        else:
            self.client.login(username, password)
            self.statusBar.showMessage("正在登录请稍后……")
            self.disableWindow()

    def disableWindow(self):
        self.txtUserName.setDisabled(True)
        self.txtPassword.setDisabled(True)
        self.btnLogin.setDisabled(True)
        self.btnCancel.setDisabled(True)

    def enableWindow(self):
        self.txtUserName.setEnabled(True)
        self.txtPassword.setEnabled(True)
        self.btnLogin.setEnabled(True)
        self.btnCancel.setEnabled(True)


if __name__ == '__main__':
    def showContactWindow(client):
        chatWin = GGChatWindow()
        chatWin.show()


    app = QApplication(sys.argv)
    client = GGChatClient(("127.0.0.1", 6767), None, None)
    loginWin = GGLoginWindow(client)
    loginWin.sigLogged.connect(showContactWindow)
    sys.exit(app.exec_())
